package org.hhqk.common;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

/**
 * @desc 表情转换
 * @author wsbg
 * */
public class EmoijUtil{
	public static String emojiConvert1(String str)  
	        throws UnsupportedEncodingException {  
	    String patternString = "([\\x{10000}-\\x{10ffff}\ud800-\udfff])";  
	  
	    Pattern pattern = Pattern.compile(patternString);  
	    Matcher matcher = pattern.matcher(str);  
	    StringBuffer sb = new StringBuffer();  
	    while(matcher.find()) {  
	        try {  
	            matcher.appendReplacement(  
	                    sb,  
	                    "[["  
	                            + URLEncoder.encode(matcher.group(1),  
	                                    "UTF-8") + "]]");  
	        } catch(UnsupportedEncodingException e) {  
//	            Log4jUtil.addProclog("ccl-web:表情：emoij",e.getMessage());  
	            throw e;  
	        }  
	    }  
	    matcher.appendTail(sb);  
//	    Log4jUtil.addProclog("ccl-web:表情：" , str + " to " + sb.toString()  
//	            + ", len：" + sb.length());  
	    return sb.toString();  
	}  
	  
	/** 
	 * @Description 还原utf8数据库中保存的含转换后emoji表情的字符串 
	 * @param str 
	 *            转换后的字符串 
	 * @return 转换前的字符串 
	 * @throws UnsupportedEncodingException 
	 *             exception 
	 */  
	public static String emojiRecovery2(String str)  
	        throws UnsupportedEncodingException {  
	    String patternString = "\\[\\[(.*?)\\]\\]";  
	  
	    Pattern pattern = Pattern.compile(patternString);  
	    Matcher matcher = pattern.matcher(str);  
	  
	    StringBuffer sb = new StringBuffer();  
	    while(matcher.find()) {  
	        try {  
	            matcher.appendReplacement(sb,  
	                    URLDecoder.decode(matcher.group(1), "UTF-8"));  
	        } catch(UnsupportedEncodingException e) {  
//	        	Log4jUtil.addProclog("ccl-web:表情："+"emojiRecovery error", e.getMessage());  
	            throw e;  
	        }  
	    }  
	    matcher.appendTail(sb);  
//	    Log4jUtil.addProclog("ccl-web:表情：", sb.toString());  
	    return sb.toString();  
	} 
	
	 public   static   String StringFilter(String   str)   throws   PatternSyntaxException   {   
		 
//		 if(StringUtil.isNull(str)){
//			 return str;
//		 }
         // 只允许字母和数字       
         // String   regEx  =  "[^a-zA-Z0-9]";                     
            // 清除掉所有特殊字符  
	   String regEx="[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]";  
	   Pattern   p   =   Pattern.compile(regEx);     
	   Matcher   m   =   p.matcher(str);     
	   return   m.replaceAll("").trim();     
   }     
       public static void main(String[] args){
    	   String   str   =   "*adCVs*34_a _09_b5*[/435^*&城池()^$$&*).{}+.|.)%%*(*.中国}34{45[]12.fd'*&999下面是中文的字符￥……{}【】。，；’“‘”？";     
    	   System.out.println(str);     
    	   System.out.println(StringFilter(str));     
		
	} 
 
}
